package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 会员管理
 **/

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;

    /**
     * 分页查询会员管理列表
     * @param queryPageBean 分页bean
     * @return 分页数据
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return memberService.findPage(queryPageBean);
    }

    /**
     * 用过id回显会员数据
     * @param id 会员id
     * @return 单个会员数据
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            Member data = memberService.findById(id);
            return new Result(true, "回显会员数据成功",data);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "回显会员数据失败");
        }
    }

    /**
     * 编辑会员
     * @return 无
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
            return new Result(true, "编辑会员成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "编辑会员失败");
        }
    }

    /**
     * 添加会员
     * @param member 提交的表单数据
     * @return 验证唯一性,提示信息
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try {

            memberService.add(member);

            return new Result(true, "添加会员成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "添加会员失败");
        }
    }

    /**
     * 手机号唯一性验证
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/phoneValidator")
    public String phoneValidator(@RequestBody Member member){
        String msg = memberService.phoneValidator(member);
        if(msg.equals("手机号已经存在")){
            return msg;
        }else {
            return msg;
        }

    }

    /**
     * 档案号唯一性验证
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/fileNumberValidator")
    public String fileNumberValidator(@RequestBody Member member){
        String msg = memberService.fileNumberValidator(member);
        if(msg.equals("档案号已经存在")){
            return msg;
        }else {
            return msg;
        }

    }

    /**
     * 身份证号唯一性验证
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/idCardValidator")
    public String idCardValidator(@RequestBody Member member){
        String msg = memberService.idCardValidator(member);
        if(msg.equals("身份证已经存在")){
            return msg;
        }else {
            return msg;
        }

    }

    /**
     * 邮箱唯一性验证
     * @param member 提交的数据
     * @return 返回验证
     */
    @RequestMapping("/emailValidator")
    public String emailValidator(@RequestBody Member member){
        String msg = memberService.emailValidator(member);
        if(msg.equals("邮箱已经存在")) {
            return msg;
        }else {
            return msg;
        }
    }

    /**
     * 分页查询体检上传列表
     * @param queryPageBean 分页bean
     * @return 体检上传分页数据
     */
    @RequestMapping("/findCheckPage")
    public PageResult findCheckPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.findCheckPage(queryPageBean);
    }

    /**
     * 删除体验信息
     * @param id 体检id
     * @return msg
     */
    @RequestMapping("/deleteCheck")
    public Result deleteCheck(Integer id){
        try {
            memberService.deleteCheck(id);
            return new Result(true,"删除体检成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除体检失败");
        }
    }

    /**
     * 上传体检报告
     * @param id 提交的体检id
     * @return msg
     */
    @RequestMapping("/uploadCheck")
    public Result uploadCheck(Integer id){
        try {
            orderService.uploadCheck(id);
            return new Result(true,"上传报告成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"上传报告失败");
        }
    }

    /**
     * 假上传
     */
    @RequestMapping("/upload")
    public void upload(){
        System.out.println("上传文件成功");
    }
}
